package com.tywj.panda.user.order

import android.os.Bundle
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.facade.annotation.Route
import com.tywj.panda.customer.common.view.base.MyBaseActivity
import com.tywj.panda.customer.common.view.paging.BasePagingFragmentSearch
import com.tywj.panda.user.R
import com.tywj.panda.user.route.UserRoute
import com.tywj.lib.core.common.util.ResourcesUtil
import com.tywj.lib.route.RouteParam
import kotlinx.android.synthetic.main.user_activity_order.*

/**
 * @desc 我的订单
 * @author June Yang
 * @date 2020/5/12
 */

@Suppress("DUPLICATE_LABEL_IN_WHEN")
@Route(path = UserRoute.USER_ORDER_RECORD)
class UserOrderActivity : MyBaseActivity() {

    override fun getLayoutID(): Int = R.layout.user_activity_order

    companion object {

        const val PARAM_CONDITION = "condition"

        //订单状态类型 0:全部 1:待收款 2:已收款 3:已取消
//        0 待付款 1付款 2已退款 3已取消 4已过期 5支付失败 6锁定（预留）7 已完成（预留） 99未知（预留）
        const val BILLING_ALL = ""
        const val BILLING_DUE_PAYMENT = "0"
        const val BILLING_MADE_PAYMENT = "1"
        const val BILLING_CANCELED = "2"
    }

    private var condition = ""

    //我的订单记录类型页面集合
    private val fragmentList = ArrayList<BasePagingFragmentSearch<*, *>>()

    //我的订单记录类型标题数组
    private val titleArray = arrayOfNulls<String>(4)

    //从订单详情页回来
    private var isBack = false

    override fun initVariable(savedInstanceState: Bundle?) {
        super.initVariable(savedInstanceState)

        condition = RouteParam(this).getString(PARAM_CONDITION)

        titleArray[0] = ResourcesUtil.getString(R.string.user_order_record_all)
        titleArray[1] = ResourcesUtil.getString(R.string.user_order_record_wait_for_pay)
        titleArray[2] = ResourcesUtil.getString(R.string.user_order_record_already_pay)
        titleArray[3] = ResourcesUtil.getString(R.string.user_order_record_had_refund)

        fragmentList.add(UserRoute.getOrderRecordFragment(this, BILLING_ALL))
        fragmentList.add(UserRoute.getOrderRecordFragment(this, BILLING_DUE_PAYMENT))
        fragmentList.add(UserRoute.getOrderRecordFragment(this, BILLING_MADE_PAYMENT))
        fragmentList.add(UserRoute.getOrderRecordFragment(this, BILLING_CANCELED))
    }

    @Suppress("UNCHECKED_CAST")
    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        titleLayout.setTitleText(R.string.user_order_title)

        slidingTabLayout.setViewPager(recordViewPager, titleArray, this, fragmentList as ArrayList<Fragment>)

        when (condition) {
            BILLING_ALL, "" -> recordViewPager.currentItem = 0
            BILLING_DUE_PAYMENT -> recordViewPager.currentItem = 1
            BILLING_MADE_PAYMENT -> recordViewPager.currentItem = 2
            BILLING_CANCELED -> recordViewPager.currentItem = 3
        }
    }

    override fun onResumeEveryTime() {
        super.onResumeEveryTime()

        // 从记录详情页回来或用户切换应用到本App，都需要刷新一次数据
        if (isBack) {
            fragmentList.forEach {
                if (it.isInitialized()) {
                    it.refreshData()
                }
            }
        }
    }

    override fun onPause() {
        super.onPause()

        isBack = true
    }
}